package net.togogo.egouweb1238.core.controller;

import java.util.List;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.image.ImageCaptchaService;

import net.togogo.egouweb1238.common.utils.Constants;
import net.togogo.egouweb1238.common.utils.Md5Pwd;
import net.togogo.egouweb1238.common.utils.ResponseUtils;
import net.togogo.egouweb1238.common.web.session.SessionProvider;
import net.togogo.egouweb1238.core.domain.area.City;
import net.togogo.egouweb1238.core.domain.area.Province;
import net.togogo.egouweb1238.core.domain.area.Town;
import net.togogo.egouweb1238.core.domain.user.Buyer;
import net.togogo.egouweb1238.core.query.area.CityQuery;
import net.togogo.egouweb1238.core.query.area.TownQuery;
import net.togogo.egouweb1238.core.service.area.ICityService;
import net.togogo.egouweb1238.core.service.area.IProvinceService;
import net.togogo.egouweb1238.core.service.area.ITownService;
import net.togogo.egouweb1238.core.service.user.IBuyerService;


/**
 * 个人行为操作：
 * 跳转登陆页面
 * 登陆
 * 个人资料
 * 收货地址
 * @author stone
 *
 */
@Controller
public class ProfileController {

	@Resource
	private SessionProvider sessionProvider;

	@Resource
	private IBuyerService buyerService;
	@Resource
	private ImageCaptchaService imageCaptchaService;
	
	@Resource
	private IProvinceService provinceService;
	@Resource
	private ICityService cityService;
	@Resource
	private ITownService townService;

	//GET
	@RequestMapping(value = "/shopping/toLogin.thtml", method = RequestMethod.GET)
	public String toLogin() {
		return "buyer/login";
	}

	//POST
	/**
	 * 1:验证码是否为null
		2:验证码  是否正确
		3:用户是否为NUll
		4:密码是否为NUll
		5:用户是否正确
		6密码是否正确
		Md5  纯生Md5 
		放进Session   跳转ReturnUrl
	 * @param buyer
	 * @param captcha
	 * @param returnUrl
	 * @return
	 */
	@RequestMapping(value = "/shopping/login.thtml", method = RequestMethod.POST)
	public String login(Buyer buyer, String captcha, String returnUrl, ModelMap model, HttpServletRequest request,
			HttpServletResponse response) {
		//验证码是否为null
		if (StringUtils.isNotBlank(captcha)) {
			//1:JSESSIONID
			//2验证码
			if (imageCaptchaService.validateResponseForID(sessionProvider.getSessionId(request, response), captcha)) {
				if (null != buyer && StringUtils.isNotBlank(buyer.getUsername())) {
					if (StringUtils.isNotBlank(buyer.getPassword())) {
						Buyer b = buyerService.getBuyerById(buyer.getUsername());
						if (null != b) {
							//
							if (b.getPassword().equals(Md5Pwd.encode(buyer.getPassword()))) {
								//把用户对象放在Session
								sessionProvider.setAttribute(request, response, Constants.BUYER_SESSION, b);
								if (StringUtils.isNotBlank(returnUrl)) {
									return "redirect:" + returnUrl;
								} else {
									//个人中心
									return "redirect:/buyer/index.thtml";
								}
							} else {
								model.addAttribute("error", "密码错误");
							}
						} else {
							model.addAttribute("error", "用户名输入错误");
						}
					} else {
						model.addAttribute("error", "请输入密码");
					}
				} else {
					model.addAttribute("error", "请输入用户名");
				}
			} else {
				model.addAttribute("error", "验证码输入错误");
			}
		} else {
			model.addAttribute("error", "请填写验证码");
		}
		return "buyer/login";
	}

	//个人中心
	@RequestMapping(value = "/buyer/index.thtml")
	public String index() {
		return "buyer/index";
	}

	//个人资料
	@RequestMapping(value = "/buyer/profile.thtml")
	public String profile(HttpServletRequest request, ModelMap model, HttpServletResponse response) {
		//加载用户
		Buyer buyer = (Buyer) sessionProvider.getAttribute(request, response, Constants.BUYER_SESSION);
		Buyer b = buyerService.getBuyerById(buyer.getUsername());
		model.addAttribute("buyer", b);
		//省
		List<Province> provinces = provinceService.getProvinceList(null);
		model.addAttribute("provinces", provinces);
		//市 
		CityQuery cityQuery = new CityQuery();
		cityQuery.setProvince(b.getProvince());
		List<City> citys = cityService.getCityList(cityQuery);
		model.addAttribute("citys", citys);
		//县
		TownQuery townQuery = new TownQuery();
		townQuery.setCity(b.getCity());
		List<Town> towns = townService.getTownList(townQuery);
		model.addAttribute("towns", towns);

		return "buyer/profile";
	}

	//
	@RequestMapping(value = "/buyer/city.thtml")
	public void city(String code, HttpServletResponse response) {
//		CityQuery cityQuery = new CityQuery();
//		cityQuery.setProvince(code);
//		List<City> citys = cityService.getCityList(cityQuery);
//
//		JSONObject jo = new JSONObject();
//		jo.put("citys", citys);
//		ResponseUtils.renderJson(response, jo.toString());

	}

	//收货地址
	@RequestMapping(value = "/buyer/deliver_address.thtml")
	public String address() {

		return "buyer/deliver_address";
	}

	
}
